package app.fichajes.data.parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import app.fichajes.data.News;

import com.gi.libraryutility.parser.BaseFeedParser;

public class FacadeNoticiasRSSLaSexta extends BaseFeedParser {
	
	public static final int MAX_NOTICIAS = 10;
	public static int numeroNoticiaActual = 0;
	
	private static final String TAG_ITEM = "item";
	private static final String TAG_TITLE = "title";
	private static final String TAG_LINK = "link";
	private static final String TAG_DESCRIPTION = "description";
	private static final String TAG_PUB_DATE = "pubDate";
	private static final String STRING_BEGIN_IMAGE = "<img src=\"";
	private static final String STRING_END_IMAGE = "/>";
	private static final String STRING_BEGIN_URL_IMAGE = "http://";
	private static final String STRING_SPACE_WHITE_URL = "\" ";
	
	private List<News> listNews;
	private News newsActual;
	//private String tag = "XmlPullNewsParser";
	private boolean isItemFound = false;
	

	
	public FacadeNoticiasRSSLaSexta(String feedUrl) throws Exception {
		super(feedUrl);
		super.parse();
	}

	
	@Override
	protected void startDocument(XmlPullParser parser) {
		listNews = new ArrayList<News>();
	}
	
	
	@Override
	protected void inicializeTag(XmlPullParser parser)throws XmlPullParserException, IOException {
		
		if(numeroNoticiaActual != MAX_NOTICIAS){
			String name = parser.getName();
			if(name.equalsIgnoreCase(TAG_ITEM)){
				isItemFound = true;
				newsActual = new News();
			}else if(name.equalsIgnoreCase(TAG_TITLE) && isItemFound){
				newsActual.setTitular(parser.nextText());
			}else if(name.equalsIgnoreCase(TAG_LINK) && isItemFound){
				//NewsActual.setLink(parser.nextText());
			}else if(name.equalsIgnoreCase(TAG_DESCRIPTION) && isItemFound){
				evaluateNewsDescription(parser.nextText());
			}else if(name.equalsIgnoreCase(TAG_PUB_DATE) && isItemFound){
				/*try {
					NewsActual.setFecha(parser.nextText());	
				} catch (Exception e) {
					
					e.printStackTrace();
				}*/
			}
		}
	}

	@Override
	protected void finalizeTag(XmlPullParser parser) {
		if(numeroNoticiaActual != MAX_NOTICIAS){
			String name = parser.getName();
			if(name.equalsIgnoreCase(TAG_ITEM)){
				if(newsActual != null){
					listNews.add(newsActual);
				}
				isItemFound = false;
				
				numeroNoticiaActual++;
			}
		}
	}
	
	private void evaluateNewsDescription(String description){
		String content = description;
		if(description.contains(STRING_BEGIN_IMAGE)){
			//Recogemos la imagen
			String image = description.substring(description.indexOf(STRING_BEGIN_IMAGE), description.indexOf(STRING_END_IMAGE)+STRING_END_IMAGE.length());
			String urlImage = image.substring(description.indexOf(STRING_BEGIN_URL_IMAGE), description.indexOf(STRING_SPACE_WHITE_URL));
			newsActual.setFoto(urlImage);
			//newsActual.setFotoGrande(urlImage);
			content = description.substring(description.indexOf(STRING_END_IMAGE)+STRING_END_IMAGE.length());
		}
		newsActual.setEntradilla(content);
		newsActual.setDescripcion(content);
		newsActual.setTexto(content);
	}

	
	@Override
	protected void text(XmlPullParser parser) {}
	
	
	@Override
	protected void endDocument(XmlPullParser parser) {}
	
	public List<News> getNoticias(){
		return listNews;
	}

}
